package com.a0soft.gphone.aTruffleHog.analytic;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.pref.Pref;
import com.a0soft.gphone.aTruffleHog.util.CONFIGURATION;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class AnalyticsUtil {
    public static final String PAGE_DEVICE_INFO = "/DeviceInfo";   
    public static final String PAGE_STATISTICS = "/Stats";   
    public static final String PAGE_STAT_OS_MY = "/Stats/OS/My";   
    public static final String PAGE_STAT_OS_ALL = "/Stats/OS/All";   
    public static final String PAGE_STAT_OS_USER_DEFINED = "/Stats/OS/UserDefined"; 
    public static final String PAGE_STAT_ROOTED_MY = "/Stats/Rooted/My"; 
    public static final String PAGE_STAT_ROOTED_ALL = "/Stats/Rooted/All"; 
    public static final String PAGE_STAT_ROOTED_USER_DEFINED = "/Stats/Rooted/UserDefined";  
    public static final String PAGE_STAT_TOP_DEVICE_MY = "/Stats/TopDevice/My";   
    public static final String PAGE_STAT_TOP_DEVICE_ALL = "/Stats/TopDevice/All"; 
    public static final String PAGE_STAT_TOP_OPERATOR_MY = "/Stats/TopOperator/My";   
    public static final String PAGE_STAT_TOP_OPERATOR_ALL = "/Stats/TopOperator/All"; 
    
    public static final String PAGE_STAT_FIND_BY_CAMERA = "/Stats/FindByCamera"; 
    public static final String PAGE_STAT_FIND_BY_CPU = "/Stats/FindByCpu";
    
    public static final String PAGE_PREF = "/Pref";   
    
    private Context m_ctx;
    private boolean m_bStart;
    private GoogleAnalyticsTracker m_tracker;
    
    private static AnalyticsUtil m_inst;
    
    private static final int VISITOR_LEVEL_SCOPE = 1;
    //private static final int SESSION_LEVEL_SCOPE = 2;
    //private static final int PAGE_LEVEL_SCOPE = 3;
    
    private static final String LOG_TAG = AnalyticsUtil.class.getSimpleName();
    
    //////////////////////////////////////////////
    public static AnalyticsUtil
    GetInstance(Context ctx)
    {
        if (m_inst == null)
            m_inst = new AnalyticsUtil(ctx);
        return m_inst;
    }

    public final boolean
    IsStarted()
    {
        return m_bStart;
    }
    
    public final void
    Start()
    {
        if (m_bStart)
            return;
        if (!Pref.IsAgreeAgreement(m_ctx))
            return;
        if (TextUtils.isEmpty(CONFIGURATION.GOOGLE_ANALYTICS_UA))
            return;

        new AsyncTask<Void, Void, Void>() {
            @Override protected Void 
            doInBackground(Void... voids) 
            {
                try {
                    GoogleAnalyticsTracker tracker = _GetTracker();
                    tracker.startNewSession(CONFIGURATION.GOOGLE_ANALYTICS_UA, 60, m_ctx); //1-min

                    DeviceInfo di = DeviceInfo.GetInstance(m_ctx);
                    tracker.setCustomVar(1, "app_version", 
                                         m_ctx.getString(R.string.app_version_code), VISITOR_LEVEL_SCOPE); 
                    tracker.setCustomVar(2, "api_level", 
                                         di.GetString(DeviceInfoKey.K.ST_SDK_INT), VISITOR_LEVEL_SCOPE);
                    tracker.setCustomVar(3, "api_name", 
                                         di.GetString(DeviceInfoKey.K.ST_RELEASE), VISITOR_LEVEL_SCOPE);
                    tracker.setCustomVar(4, "model_id", 
                                         di.GetString(DeviceInfoKey.K.ST_MID), VISITOR_LEVEL_SCOPE);
                    
                    tracker.dispatch();
                } catch (Exception e) {}
                return null;
            }
            
            @Override protected void 
            onPreExecute()
            {
                m_bStart = true;
                if (aDebug.ENABLED)
                    aDebug.d(LOG_TAG, "Start()");
            }
        }.execute();
    }
    
    public final void
    Stop()
    {
        if (!m_bStart)
            return;
        if (TextUtils.isEmpty(CONFIGURATION.GOOGLE_ANALYTICS_UA))
            return;
        
        new AsyncTask<Void, Void, Void>() {
            @Override protected Void 
            doInBackground(Void... voids) 
            {
                try {
                    GoogleAnalyticsTracker tracker = _GetTracker();
                    tracker.dispatch();
                    tracker.stopSession();
                } catch (Exception e) {}
                return null;
            }

            @Override protected void 
            onPostExecute(Void result)
            {
                m_bStart = false;
                if (aDebug.ENABLED)
                    aDebug.d(LOG_TAG, "Stop()");
            }
        }.execute();
    }
    
    public void
    TrackPageView(String strPageName)
    {
        if (!m_bStart)
            return;
        if (TextUtils.isEmpty(CONFIGURATION.GOOGLE_ANALYTICS_UA))
            return;
        
        new AsyncTask<String, Void, Void>() {
            @Override protected Void 
            doInBackground(String... strPages) 
            {
                try {
                    GoogleAnalyticsTracker tracker = _GetTracker();
                    for (String strPage: strPages)
                        tracker.trackPageView(strPage);                    
                    //tracker.dispatch();
                } catch (Exception e) {}
                return null;
            }
        }.execute(strPageName);
    }
    
    private
    AnalyticsUtil(Context ctx)
    {
        m_ctx = ctx;
    }
    
    private GoogleAnalyticsTracker
    _GetTracker()
    {
        if (m_tracker == null)
            m_tracker = GoogleAnalyticsTracker.getInstance();
        return m_tracker;
    }
}
